//
//  ZCItemModelContent.m
//
//  Created by   on 15/11/27
//  Copyright (c) 2015 __MyCompanyName__. All rights reserved.
//

#import "ZCItemModelContent.h"


NSString *const kZCItemModelContentItemsname = @"itemsname";
NSString *const kZCItemModelContentContent = @"content";
NSString *const kZCItemModelContentInformation = @"information";
NSString *const kZCItemModelContentId = @"id";
NSString *const kZCItemModelContentPrice = @"price";
NSString *const kZCItemModelContentClassify = @"classify";
NSString *const kZCItemModelContentInstitutionid = @"institutionid";
NSString *const kZCItemModelContentClassifyid = @"classifyid";
NSString *const kZCItemModelContentName = @"name";
NSString *const kZCItemModelContentItems = @"items";
NSString *const kZCItemModelContentchecked = @"checked";

NSString *const kZCItemModelContentIschecked = @"ischecked";



@interface ZCItemModelContent ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ZCItemModelContent

@synthesize itemsname = _itemsname;
@synthesize content = _content;
@synthesize information = _information;
@synthesize contentIdentifier = _contentIdentifier;
@synthesize price = _price;
@synthesize classify = _classify;
@synthesize institutionid = _institutionid;
@synthesize classifyid = _classifyid;
@synthesize name = _name;
@synthesize items = _items;
@synthesize checked = _checked;
@synthesize ischecked = _ischecked;

+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.itemsname = [self objectOrNilForKey:kZCItemModelContentItemsname fromDictionary:dict];
            self.content = [self objectOrNilForKey:kZCItemModelContentContent fromDictionary:dict];
            self.information = [self objectOrNilForKey:kZCItemModelContentInformation fromDictionary:dict];
            self.contentIdentifier = [self objectOrNilForKey:kZCItemModelContentId fromDictionary:dict];
            self.price = [self objectOrNilForKey:kZCItemModelContentPrice fromDictionary:dict];
            self.classify = [self objectOrNilForKey:kZCItemModelContentClassify fromDictionary:dict];
            self.institutionid = [self objectOrNilForKey:kZCItemModelContentInstitutionid fromDictionary:dict];
            self.classifyid = [self objectOrNilForKey:kZCItemModelContentClassifyid fromDictionary:dict];
            self.name = [self objectOrNilForKey:kZCItemModelContentName fromDictionary:dict];
            self.ischecked = [[self objectOrNilForKey:kZCItemModelContentIschecked fromDictionary:dict]doubleValue];
            self.items = [self objectOrNilForKey:kZCItemModelContentItems fromDictionary:dict];
            self.checked = YES;

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.itemsname forKey:kZCItemModelContentItemsname];
    [mutableDict setValue:self.content forKey:kZCItemModelContentContent];
    [mutableDict setValue:self.information forKey:kZCItemModelContentInformation];
    [mutableDict setValue:self.contentIdentifier forKey:kZCItemModelContentId];
    [mutableDict setValue:self.price forKey:kZCItemModelContentPrice];
    [mutableDict setValue:self.classify forKey:kZCItemModelContentClassify];
    [mutableDict setValue:self.institutionid forKey:kZCItemModelContentInstitutionid];
    [mutableDict setValue:self.classifyid forKey:kZCItemModelContentClassifyid];
    [mutableDict setValue:self.name forKey:kZCItemModelContentName];
    NSMutableArray *tempArrayForItems = [NSMutableArray array];
    for (NSObject *subArrayObject in self.items) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForItems addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForItems addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForItems] forKey:kZCItemModelContentItems];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.itemsname = [aDecoder decodeObjectForKey:kZCItemModelContentItemsname];
    self.content = [aDecoder decodeObjectForKey:kZCItemModelContentContent];
    self.information = [aDecoder decodeObjectForKey:kZCItemModelContentInformation];
    self.contentIdentifier = [aDecoder decodeObjectForKey:kZCItemModelContentId];
    self.price = [aDecoder decodeObjectForKey:kZCItemModelContentPrice];
    self.classify = [aDecoder decodeObjectForKey:kZCItemModelContentClassify];
    self.institutionid = [aDecoder decodeObjectForKey:kZCItemModelContentInstitutionid];
    self.classifyid = [aDecoder decodeObjectForKey:kZCItemModelContentClassifyid];
    self.name = [aDecoder decodeObjectForKey:kZCItemModelContentName];
    self.items = [aDecoder decodeObjectForKey:kZCItemModelContentItems];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_itemsname forKey:kZCItemModelContentItemsname];
    [aCoder encodeObject:_content forKey:kZCItemModelContentContent];
    [aCoder encodeObject:_information forKey:kZCItemModelContentInformation];
    [aCoder encodeObject:_contentIdentifier forKey:kZCItemModelContentId];
    [aCoder encodeObject:_price forKey:kZCItemModelContentPrice];
    [aCoder encodeObject:_classify forKey:kZCItemModelContentClassify];
    [aCoder encodeObject:_institutionid forKey:kZCItemModelContentInstitutionid];
    [aCoder encodeObject:_classifyid forKey:kZCItemModelContentClassifyid];
    [aCoder encodeObject:_name forKey:kZCItemModelContentName];
    [aCoder encodeObject:_items forKey:kZCItemModelContentItems];
}

- (id)copyWithZone:(NSZone *)zone
{
    ZCItemModelContent *copy = [[ZCItemModelContent alloc] init];
    
    if (copy) {

        copy.itemsname = [self.itemsname copyWithZone:zone];
        copy.content = [self.content copyWithZone:zone];
        copy.information = [self.information copyWithZone:zone];
        copy.contentIdentifier = [self.contentIdentifier copyWithZone:zone];
        copy.price = [self.price copyWithZone:zone];
        copy.classify = [self.classify copyWithZone:zone];
        copy.institutionid = [self.institutionid copyWithZone:zone];
        copy.classifyid = [self.classifyid copyWithZone:zone];
        copy.name = [self.name copyWithZone:zone];
        copy.items = [self.items copyWithZone:zone];
    }
    
    return copy;
}


@end
